//
//  ESortWebViewController.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/6.
//  Copyright © 2016年 XuRui. All rights reserved.
//

//webView交互 放大缩小字体
#import "ESortWebViewController.h"

@interface ESortWebViewController ()<UIWebViewDelegate>
@property (nonatomic, retain) UIWebView *wv; /**< webView */
@property (nonatomic, assign) NSInteger textSize; /**< 字体初始值 */
@property (nonatomic, retain) NSString *text; /**< 字体 */
@property (nonatomic, assign) BOOL isNight; /**< 夜间转换 */
@end

@implementation ESortWebViewController

#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    _wv.delegate = nil;
    [_DetailID release];
    [_wv release];
    [_text release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"礼遇详情";
    
    //放大字体
    UIBarButtonItem *r1 = [[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"textBig"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(big:)]autorelease];
    //缩小字体
    UIBarButtonItem *r2 = [[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"textSm"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(small:)] autorelease];
    //夜间
    UIBarButtonItem *r3 = [[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"night"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(night:)] autorelease];
    
    self.navigationItem.rightBarButtonItems = @[r1, r2,r3];
    
    self.textSize = 100;

    [self createSubView];

}


#pragma mark - ------------ 创建web子视图 ---------------
- (void)createSubView
{
    //UIWebView 加载网页数据 直接适配屏幕
    self.wv = [[UIWebView alloc]initWithFrame:CGRectMake(0, -50,self.view.frame.size.width, self.view.frame.size.height + 50)];
    _wv.delegate = self;
    _wv.scalesPageToFit = YES;
    _wv.userInteractionEnabled = YES;
    //加载网址
    NSString *html = [NSString stringWithFormat:@"https://enjoy.ricebook.com/product/%@", _DetailID];
    [_wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:html]]];
    [self.view addSubview:_wv];
    
/** 添加捏合手势 */
    UIPinchGestureRecognizer *pich = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pichWV)];
    [_wv addGestureRecognizer:pich];
    
    [_wv release];
    [pich release];
}
#pragma mark - ------------ 放大字体 ---------------
- (void)big:(UIBarButtonItem *)item
{

    _textSize += 20;
    self.text = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%ld%%'", _textSize];
    [_wv stringByEvaluatingJavaScriptFromString:_text];
}

#pragma mark - ------------ 缩小字体 ---------------
- (void)small:(UIBarButtonItem *)item
{
    
    _textSize -= 20;
    self.text = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%ld%%'", _textSize];
    //字体大小
    [_wv stringByEvaluatingJavaScriptFromString:_text];
}

#pragma mark - ------------ 放大缩小界面 ---------------
- (void)pichWV
{

  //(initial-scale是初始缩放比,minimum-scale=1.0最小缩放比,maximum-scale=5.0最大缩放比,user-scalable=yes是否支持缩放)
    NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=self.view.frame.size.width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0, user-scalable=yes\""];
    [_wv stringByEvaluatingJavaScriptFromString:meta];
  
}

#pragma mark - ------------ 夜间 ---------------
- (void)night:(UIBarButtonItem *)item
{
    self.isNight = !self.isNight;
    if (self.isNight) {
        
    //页面灰色
    [_wv stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#BEBEBE'"];
    }else{
    
    [_wv stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#FFFFFF'"];
    }
}


#pragma mark - ------------ tab隐藏 ---------------
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    self.tabBarController.tabBar.hidden = YES;
    
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
